#include "IndividualConfig.h"
#include "json/json.h"
#include <algorithm>
#include <fstream>

using namespace std;

CIndividualConfig g_IndividualConfig;

std::string ReadContentsFromFile(const std::string& filename)
{
	std::fstream fs;
	fs.open(filename);
	if (!fs.is_open())
		return "";

	std::ostringstream oss;
	oss << fs.rdbuf();
	fs.close();
	return oss.str();
}

void CIndividualConfig::Init()
{
	ReadFuncWithAnsIds("dd50_id_val.json");
}

void CIndividualConfig::ReadFuncWithAnsIds(const std::string& configFile)
{
	string contents = ReadContentsFromFile(configFile);

	Json::Value jVal;
	Json::Reader jReader;
	if (!jReader.parse(contents, jVal))
	{
		return;
	}

	auto allFuncs = jVal.getMemberNames();

	for (const auto& funcno : allFuncs)
	{
		vector<int> ids;
		const auto& readIds = jVal[funcno];
		for (Json::ArrayIndex i = 0; i < readIds.size(); ++i)
		{
			ids.push_back(readIds[i].asInt());
		}
		m_FuncWithAnsIds.emplace(stoi(funcno), ids);
	}
}

const std::vector<int>& CIndividualConfig::GetAnsIds(int funcno)
{
	return m_FuncWithAnsIds[funcno];
}